package com.sdp.irrigation.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.db.irrigation_expert.service.IIrrigationExpertService;
import com.sdp.irrigation.entity.IrrigationExpert;

/**
 * <p>
 * 专家信息 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2025-02-02
 */
@RestController
@RequestMapping("/IrrigationExpertController")
public class IrrigationExpertController {

	@Autowired
	private IIrrigationExpertService iIrrigationExpertService;

	@Autowired
	private Query query;

	
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iIrrigationExpertService, IrrigationExpert.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody IrrigationExpert expert) {
		if(expert==null) {
			return Result.error("专家信息为空");
		}
		if(StringUtils.isEmpty(expert.getKeyId())) {
			
			expert.setCreateTime(LocalDateTime.now());
			expert.setUpdateTime(LocalDateTime.now());
			iIrrigationExpertService.save(expert);
		}else {
			expert.setUpdateTime(LocalDateTime.now());
			iIrrigationExpertService.updateById(expert);
		}
		return Result.ok();
	}
	
	

	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iIrrigationExpertService.removeById(keyId);
		
		return Result.ok();
	}
}

